package com.example.demo.controller;

import com.example.demo.annotation.UserLoginToken;
import com.example.demo.service.InfoService;
import com.example.demo.utils.Result;
import com.example.demo.utils.ResultCode;
import com.example.demo.utils.seleniumUtil;
import com.example.demo.utils.tokenUtil;
import com.example.demo.vo.InfoVo;
import org.apache.poi.ss.formula.functions.T;
import org.apache.tomcat.util.http.fileupload.IOUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.List;
import java.util.UUID;

import static com.example.demo.utils.tokenUtil.getToken;

@Controller
public class hellocontroller {
    @Autowired
    private InfoService infoService;


    @RequestMapping("/hello")
    @ResponseBody
    public Result hello(){
        List<InfoVo> infoVo = infoService.getInfo();
//        String token = getToken(infoVo.get(0));
//        System.err.println(token);
//        System.err.println(infoVo);
        return Result.success(infoVo);
    }

//    @RequestMapping("/testToken")
//    @ResponseBody
//    @UserLoginToken
//    public Result testToken(){
//        List<InfoVo> infoVo = infoService.getInfo();
//        String token = getToken(infoVo.get(0));
//        System.err.println(token);
//        return Result.success(token);
//    }

    @RequestMapping("/openbrowser")
    public String openbrowser(){
        seleniumUtil sutil = new seleniumUtil();
        sutil.openBrowser();
        return "请求成功";
    }

    @RequestMapping(method = RequestMethod.POST,value = "setInfo")
    @ResponseBody
    public String setInfo(@RequestBody InfoVo infoVo){
        infoService.setInfo(infoVo);
        return "请求成功";
    }

    @RequestMapping(method = RequestMethod.POST,value = "upload")
    @ResponseBody
    public Result upload(@RequestParam("file") MultipartFile file, HttpServletRequest request){
        if(!file.isEmpty()){
            String fileName = file.getOriginalFilename();
            String sfileName = fileName.substring(fileName.lastIndexOf("."));
            String pfileName = UUID.randomUUID() + sfileName;
            String filePath = "d:/upload/";

//            File dir =new File(filePath);
//            //如果文件夹不存在则创建
//            if  (!dir.exists()  && !dir .isDirectory()) {
//                System.out.println("//不存在");
//                dir.mkdir();
//            }
            File dest = new File(filePath + pfileName);
            try {
                file.transferTo(dest);
                String p = request.getScheme() + "://" + request.getServerName()
                        + ":" + request.getServerPort() +"/file/"+ pfileName;
                System.err.println(p);
                return Result.success(p);
            } catch (IOException e) {
                System.err.println(e);
                return Result.failure(ResultCode.FAILURE,e);
            }

        }else {
            return Result.failure(ResultCode.FAILURE);
        }
    }
}
